package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Menu;
import org.ahuanlien.crm.service.IMenuService;
import org.ahuanlien.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {

	@Autowired
	private IMenuService menuService;


	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "menu/view";
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Menu menu) {
		try {
			menuService.saveOrUpdate(menu);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			menuService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	@RequestMapping("selectRootMenu")
	@ResponseBody
	public List<Menu> selectRootMenu() {
		List<Menu> rootMenus = menuService.selectRootMenu();
		return rootMenus;
	}

	@RequestMapping("selectMenuByEmployee")
	@ResponseBody
	public List<Menu> selectMenuByEmployee() {
		List<Menu> rootMenus = menuService.selectMenuByEmployee();
		return rootMenus;
	}
}
